package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/account")
@SessionAttributes({"account","cart","myList", "authenticated"})
public class AccountController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private CartService cartService;
    private static final List<String> LANGUAGE_LIST;
    private static final List<String> CATEGORY_LIST;
    static {
        List<String> langList = new ArrayList<String>();
        langList.add("ENGLISH");
        langList.add("CHINESE");
        LANGUAGE_LIST = Collections.unmodifiableList(langList);
        List<String> catList = new ArrayList<String>();
        catList.add("FISH");
        catList.add("DOGS");
        catList.add("REPTILES");
        catList.add("CATS");
        catList.add("BIRDS");
        CATEGORY_LIST = Collections.unmodifiableList(catList);
    }

    @GetMapping("/signOnForm")
    public String signonForm() {
        return "account/SignOnForm";
    }


    /*username 在数据库不唯一 而返回值只要一个*/
    @PostMapping("/signOn")
    public String signon(String username, String password, Model model) {
        Account account = accountService.getAccount(username, password);
        Cart cart=new Cart();
        if (account == null) {
            String msg = "Invalid username or password.  Signon failed.";
            model.addAttribute("msg", msg);
            return "account/SignOnForm";
        } else {
            account.setPassword(null);
            List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
            boolean authenticated = true;
            cart=cartService.getCart(username);
            model.addAttribute("cart",cart);
            model.addAttribute("account", account);
            model.addAttribute("myList", myList);
            model.addAttribute("authenticated", authenticated);
            return "catalog/Main";
        }
    }

    @GetMapping("/signOff")
    public String signoff(Model model) {
        model.addAttribute("account", null);
        model.addAttribute("myList", null);
        model.addAttribute("cart",null);
        model.addAttribute("authenticated", false);
        return "catalog/Main";
    }

    @GetMapping("/editAccountForm")
    public String editAccountForm(@SessionAttribute("account") Account account , Model model) {
        model.addAttribute("account", account);
        model.addAttribute("LANGUAGE_LIST", LANGUAGE_LIST);
        model.addAttribute("CATEGORY_LIST", CATEGORY_LIST);
        return "account/EditAccountForm";
    }

    @PostMapping("/saveAccount")
    public String editAccount(Account account, String repeatedPassword, Model model) {
        if (account.getPassword() == null || account.getPassword().length() == 0 || repeatedPassword == null || repeatedPassword.length() == 0) {
            String msg = "密码不能为空";
            model.addAttribute("msg", msg);
            return "account/EditAccountForm";
        } else if (!account.getPassword().equals(repeatedPassword)) {
            String msg = "两次密码不一致";
            model.addAttribute("msg", msg);
            return "account/EditAccountForm";
        } else {
            accountService.updateAccount(account);
            account = accountService.getAccount(account.getUsername());
            List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
            boolean authenticated = true;
            model.addAttribute("account", account);
            model.addAttribute("myList", myList);
            model.addAttribute("authenticated", authenticated);
            return "redirect:/catalog/index";
        }
    }

    @GetMapping("/newAccountForm")
    public String newAccountForm(Model model){
        model.addAttribute("newAccount",new Account());
        model.addAttribute("LANGUAGE_LIST", LANGUAGE_LIST);
        model.addAttribute("CATEGORY_LIST", CATEGORY_LIST);
        return "account/NewAccountForm";
    }

    @PostMapping("/newAccount")
    public String newAccount(Account newAccount,  String repeatedPassword,Model model) {
        //用户名重复暂时已解决
        Account tempaccount = new Account();
        tempaccount = accountService.getAccount(newAccount.getUsername());
        if (newAccount.getUsername() == null) {
            String msg = "请输入用户名";
            model.addAttribute("msg", msg);
            newAccount.setUsername("");
            boolean authenticated = false;
            model.addAttribute("newAccount", newAccount);
            model.addAttribute("authenticated", authenticated);
            model.addAttribute("msg", msg);
            return "account/NewAccountForm";
        }
        if (tempaccount != null) {
            String msg = "用户名已存在";
            newAccount.setUsername("");
            boolean authenticated = false;
            model.addAttribute("newAccount", newAccount);
            model.addAttribute("authenticated", authenticated);
            model.addAttribute("msg", msg);
            return "account/NewAccountForm";
        } else if (newAccount.getPassword() == null || newAccount.getPassword().length() == 0 || repeatedPassword == null || repeatedPassword.length() == 0) {
            String msg = "密码不能为空";
            newAccount.setUsername("");
            boolean authenticated = false;
            model.addAttribute("newAccount", newAccount);
            model.addAttribute("authenticated", authenticated);
            model.addAttribute("msg", msg);
            return "account/NewAccountForm";
        } else if (!newAccount.getPassword().equals(repeatedPassword)) {
            String msg = "两次密码不一致";
            newAccount.setUsername("");
            boolean authenticated = false;
            model.addAttribute("newAccount", newAccount);
            model.addAttribute("authenticated", authenticated);
            model.addAttribute("msg", msg);
            return "account/NewAccountForm";
        } else {
            accountService.insertAccount(newAccount);
            List<Product> myList = catalogService.getProductListByCategory(newAccount.getFavouriteCategoryId());
            boolean authenticated = true;
            model.addAttribute("account", newAccount);
            model.addAttribute("myList", myList);
            model.addAttribute("authenticated", authenticated);
            return "redirect:/catalog/index";
        }
    }
}